<!--
  Copyright 2015-2019 the original author or authors.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<div style="position: relative; min-height: 100px;">
  <div ng-include src="'template/gt-http-error-overlay.html'"></div>
  <div gt-spinner
       gt-show="showSpinner && !showQueries"
       class="gt-left-aligned-spinner">
  </div>
  <div style="position: absolute; width: 100%; margin-top: 50px;">
    <div gt-spinner
         gt-show="showSpinner && showQueries">
    </div>
  </div>
  <div class="gt-panel-overlay"
       ng-if="showSpinner">
  </div>
  <div ng-if="!showSpinner && !showQueries && !httpError"
       style="display: table; width: 100%; height: 100%; margin-top: 50px;">
    <div style="display: table-cell; text-align: center; vertical-align: middle;">
      <div class="gt-chart-no-data"
           ng-hide="showOverwrittenMessage">
        No data for this time period
      </div>
      <div class="gt-chart-no-data"
           ng-if="showOverwrittenMessage">
        There was query data but it has expired
      </div>
    </div>
  </div>
  <div ng-if="showQueries">
    <table class="table gt-query-table">
      <thead>
      <tr>
        <th style="vertical-align: middle;">
          <select class="custom-select"
                  ng-model="page.queryType"
                  style="width: inherit;"
                  ng-if="queryTypes.length > 1">
            <option selected value="">(All)</option>
            <option ng-repeat="queryType in queryTypes" ng-value="queryType">{{queryType}}</option>
          </select>
        </th>
        <th scope="col"
            class="gt-query-total-column"
            ng-attr-aria-sort="{{ngAttrAriaSort('total-time')}}">
          <a ng-href="{{currentTabUrl()}}{{sortQueryString('total-time')}}" class="gt-sortable-table-header">
            Total<br>
            time<br>
            <span ng-class="sortIconClass('total-time')"
                  ng-if="sortAttribute === 'total-time'">
            </span>
            (ms)
          </a>
        </th>
        <th scope="col"
            class="gt-query-count-column"
            ng-attr-aria-sort="{{ngAttrAriaSort('execution-count')}}">
          <a ng-href="{{currentTabUrl()}}{{sortQueryString('execution-count')}}" class="gt-sortable-table-header">
            Total<br>
            <span ng-class="sortIconClass('execution-count')"
                  ng-if="sortAttribute === 'execution-count'">
            </span>
            count
          </a>
        </th>
        <th scope="col"
            class="gt-query-other-column"
            ng-attr-aria-sort="{{ngAttrAriaSort('time-per-execution')}}">
          <a ng-href="{{currentTabUrl()}}{{sortQueryString('time-per-execution')}}" class="gt-sortable-table-header">
            Avg<br>
            time<br>
            <span ng-class="sortIconClass('time-per-execution')"
                  ng-if="sortAttribute === 'time-per-execution'">
            </span>
            (ms)
          </a>
        </th>
        <th scope="col"
            class="gt-query-other-column"
            ng-attr-aria-sort="{{ngAttrAriaSort('rows-per-execution')}}">
          <a ng-href="{{currentTabUrl()}}{{sortQueryString('rows-per-execution')}}" class="gt-sortable-table-header">
            Avg<br>
            <span style="white-space: nowrap;">
            <span ng-class="sortIconClass('rows-per-execution')"
                  ng-if="sortAttribute === 'rows-per-execution'">
            </span>
            rows
              </span>
          </a>
        </th>
      </tr>
      </thead>
      <tbody ng-if="!smallScreen()">
      <tr ng-repeat="query in queries | filter:{queryType: page.queryType} | orderBy:sortAttr:sortAsc">
        <th scope="row" class="gt-query-text-column"
            ng-click="showQueryModal(query)"
            style="font-weight: normal;">
          {{query.text}}
        </th>
        <td class="gt-query-total-column">
          {{query.totalDurationNanos / 1000000 | gtMillis}}
        </td>
        <td class="gt-query-count-column">
          {{query.executionCount | number}}
        </td>
        <td class="gt-query-other-column">
          {{query.timePerExecution | gtMillis}}
        </td>
        <td class="gt-query-other-column">
          {{query.rowsPerExecution | gtCount}}
        </td>
      </tr>
      <tr ng-if="limitExceededBucket">
        <th scope="row" class="gt-query-text-column"
            style="font-weight: normal;">
          {{limitExceededBucket.truncatedQueryText}}
        </th>
        <td class="gt-query-total-column">
          {{limitExceededBucket.totalDurationNanos / 1000000 | gtMillis}}
        </td>
        <td class="gt-query-count-column">
          {{limitExceededBucket.executionCount | number}}
        </td>
        <td class="gt-query-other-column">
          {{limitExceededBucket.timePerExecution | gtMillis}}
        </td>
        <td class="gt-query-other-column">
          {{limitExceededBucket.rowsPerExecution | gtCount}}
        </td>
      </tr>
      </tbody>
      <!-- TODO apply scope="row" appropriately for small screens -->
      <tbody ng-if="smallScreen()">
      <tr ng-repeat-start="query in queries | filter:{queryType: page.queryType} | orderBy:sortAttr:sortAsc">
        <td class="gt-query-text-column"
            ng-click="showQueryModal(query)"
            colspan="5"
            style="padding-bottom: 4px;">
          {{query.text}}
        </td>
      </tr>
      <tr ng-repeat-end>
        <td class="border-top-0 pt-0"></td>
        <td class="gt-query-total-column border-top-0 pt-0">
          {{query.totalDurationNanos / 1000000 | gtMillis}}
        </td>
        <td class="gt-query-count-column border-top-0 pt-0">
          {{query.executionCount}}
        </td>
        <td class="gt-query-other-column border-top-0 pt-0">
          {{query.timePerExecution | gtMillis}}
        </td>
        <td class="gt-query-other-column border-top-0 pt-0">
          {{query.rowsPerExecution | gtCount}}
        </td>
      </tr>
      <tr ng-if="limitExceededBucket">
        <td class="gt-query-text-column"
            colspan="5"
            style="padding-bottom: 4px;">
          {{limitExceededBucket.text}}
        </td>
      </tr>
      <tr ng-if="limitExceededBucket">
        <td class="border-top-0 pt-0"></td>
        <td class="gt-query-total-column border-top-0 pt-0">
          {{limitExceededBucket.totalDurationNanos / 1000000 | gtMillis}}
        </td>
        <td class="gt-query-count-column border-top-0 pt-0">
          {{limitExceededBucket.executionCount}}
        </td>
        <td class="gt-query-other-column border-top-0 pt-0">
          {{limitExceededBucket.timePerExecution | gtMillis}}
        </td>
        <td class="gt-query-other-column border-top-0 pt-0">
          {{limitExceededBucket.rowsPerExecution | gtCount}}
        </td>
      </tr>
      </tbody>
    </table>
  </div>
</div>
<div class="modal gt-full-width-modal"
     role="dialog"
     tabindex="-1"
     id="queryModal">
  <div class="modal-dialog modal-dialog-centered"
       role="document">
    <div class="modal-content">
      <div class="gt-close-sticky">
        <!-- enclosing div is needed so that button clickable space doesn't consume entire page width -->
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="gt-clipboard-sticky">
        <!-- enclosing div is needed so that clipboard clickable space doesn't consume entire page width -->
        <span class="fas fa-clipboard gt-clip d-none"></span>
      </div>
      <div class="modal-body">
        <div gt-spinner
             gt-show="showModalSpinner"
             style="position: relative; display: inline-block; padding-left: 40px; padding-top: 60px;">
        </div>
        <div ng-if="queryExpired">
          <strong>
            The full text for this query has expired
          </strong>
        </div>
        <div ng-if="queryError"
             class="gt-red">
          An error occurred retrieving the query
        </div>
        <!-- display: inline-block is so that true width can be calculated and modal shrunk to fit if possible -->
        <!-- not using ng-if/hide here since need it to be visible for size calculation -->
        <pre class="gt-monospace"
             id="formattedQuery"
             style="white-space: pre-wrap; display: inline-block; margin: 0;">
      </pre>
        <div class="gt-monospace"
             id="unformattedQuery"
             style="padding-bottom: 5px;">
        </div>
        <div>
          <a href="#"
             id="formattingToggle"
             ng-if="formattedQuery"
             ng-click="toggleFormatted()"
             style="margin: 15px 0 0 20px;">
            {{ showFormatted ? '(show unformatted)' : '(show formatted)' }}
          </a>
        </div>
      </div>
    </div>
  </div>
</div>
